package com.tegtech.api.monitor.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tegtech.api.monitor.mapper.SysLoginLogMapper;
import com.tegtech.api.monitor.service.ISysLoginLogService;
import com.tegtech.starter.security.log.domain.SysLoginLog;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author Jun
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements ISysLoginLogService {

    /**
     * 查询系统登录日志集合
     *
     * @param loginlog 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLoginLog> selectLoginLogList(SysLoginLog loginlog) {
        Map<String, Object> params = loginlog.getParams();
        return list(new LambdaQueryWrapper<SysLoginLog>()
                .like(StrUtil.isNotBlank(loginlog.getIpaddr()),SysLoginLog::getIpaddr,loginlog.getIpaddr())
                .eq(StrUtil.isNotBlank(loginlog.getStatus()),SysLoginLog::getStatus,loginlog.getStatus())
                .like(StrUtil.isNotBlank(loginlog.getUserName()),SysLoginLog::getUserName,loginlog.getUserName())
                .apply(Validator.isNotEmpty(params.get("beginTime")), "date_format(login_time,'%y%m%d') &gt;= date_format({0},'%y%m%d')", params.get("beginTime"))
                .apply(Validator.isNotEmpty(params.get("endTime")), "date_format(login_time,'%y%m%d') &lt;= date_format({0},'%y%m%d'", params.get("endTime"))
                .orderByDesc(SysLoginLog::getId));
    }

    /**
     * 批量删除系统登录日志
     *
     * @param ids 需要删除的登录日志ID
     * @return
     */
    @Override
    public int deleteLoginLogByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLoginLog() {
        remove(new LambdaQueryWrapper<>());
    }
}
